package com.example.weather.Fragment;

import android.app.AlertDialog;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.weather.Base.BaseFragment;
import com.example.weather.Bean.Weatherbean;
import com.example.weather.DB.Dbmanager;
import com.example.weather.R;
import com.google.gson.Gson;
import com.squareup.picasso.Picasso;

import java.util.List;

import static android.content.Context.MODE_PRIVATE;

public class CityweatherFragment extends BaseFragment implements View.OnClickListener {
    private TextView frag_tv_currenttemp,frag_tv_city,frag_tv_condition,frag_tv_date,frag_tv_wind,frag_tv_line1,frag_tv_temprange;
    private TextView frag_index_tv_dress,frag_index_tv_washcar,frag_index_tv_cold,frag_index_tv_sport,frag_index_tv_rays;
    private ImageView frag_iv_today;
    private LinearLayout frag_center_layout,frag_layout;
    String url1 = "http://api.map.baidu.com/telematics/v3/weather?location=";
    String url2 = "&output=json&ak=FkPhtMBK0HTIQNh7gG4cNUttSTyr0nzo";
    private List<Weatherbean.ResultsBean.IndexBean> indexList;
    private String city;
    private SharedPreferences pref;
    private int bg;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view =inflater.inflate(R.layout.fragment_city_weather, container, false);
        initview(view);
        exchangebg();
        //        可以通过activity传值获取到当前fragment加载的是那个地方的天气情况
        Bundle bundle = getArguments();
        city = bundle.getString("city");
        String url=url1+city+url2;
        //调取父类的方法
        loadData(url);
        return view;
    }
    //换壁纸函数
    public void exchangebg(){
        pref = getActivity().getSharedPreferences("bg_pref", MODE_PRIVATE);
        bg = pref.getInt("bg", 2);
        switch (bg) {
            case 0:
                frag_layout.setBackgroundResource(R.mipmap.bg);
                break;
            case 1:
                frag_layout.setBackgroundResource(R.mipmap.bg2);
                break;
            case 2:
                frag_layout.setBackgroundResource(R.mipmap.bg3);
                break;
        }
    }

    @Override
    public void onSuccess(String result) {
        //解析并展示
        showData(result);
        //更新数据
        int updatacity = Dbmanager.updatacity(city, result);
        if (updatacity<=0){
            //更新失败 没有这个城市，增加城市记录
            Dbmanager.addcity(city,result);
        }
    }

    @Override
    public void onError(Throwable ex, boolean isOnCallback) {
        //数据库找上一次信息显示在fragment
        String s = Dbmanager.queryinfo(city);
        if (TextUtils.isEmpty(s)) {
            showData(s);
        }
    }

    private void showData(String result) {
        //gson解析
        Weatherbean weatherbean = new Gson().fromJson(result, Weatherbean.class);
        Weatherbean.ResultsBean resultsBean = weatherbean.getResults().get(0);
        //获取指数信息
        indexList = resultsBean.getIndex();
        //设置textview
        frag_tv_date.setText(weatherbean.getDate());
        frag_tv_city.setText(resultsBean.getCurrentCity());
        //获取今天天气情况
        Weatherbean.ResultsBean.WeatherDataBean todayDataBean = resultsBean.getWeather_data().get(0);
        frag_tv_wind.setText(todayDataBean.getWind());
        frag_tv_temprange.setText(todayDataBean.getTemperature());
        frag_tv_condition.setText(todayDataBean.getWeather());
        //获取实时温度
        String[] strings = todayDataBean.getDate().split("：");
        String todaywd = strings[1].replace(")", "");
        frag_tv_currenttemp.setText(todaywd);
        //设置显示天气的图片
        Picasso.with(getActivity()).load(todayDataBean.getDayPictureUrl()).into(frag_iv_today);
        //获取未来三天的天气情况
        List<Weatherbean.ResultsBean.WeatherDataBean> futurelist = resultsBean.getWeather_data();
        futurelist.remove(0);
        for (int i = 0; i < futurelist.size(); i++) {
            View view = LayoutInflater.from(getActivity()).inflate(R.layout.item_main_center, null);
            view.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT));
            frag_center_layout.addView(view);
            TextView datatv=view.findViewById(R.id.item_center_tv_date);
            TextView contv=view.findViewById(R.id.item_center_tv_con);
            TextView temptv=view.findViewById(R.id.item_center_tv_temp);
            ImageView img=view.findViewById(R.id.item_center_iv);
            //获取对应位置的天气情况
            Weatherbean.ResultsBean.WeatherDataBean databean = futurelist.get(i);
            datatv.setText(databean.getDate());
            contv.setText(databean.getWeather());
            temptv.setText(databean.getTemperature());
            Picasso.with(getActivity()).load(databean.getDayPictureUrl()).into(img);
        }

    }

    private void initview(View view) {
        frag_tv_currenttemp=view.findViewById(R.id.frag_tv_currenttemp);
        frag_tv_city=view.findViewById(R.id.frag_tv_city);
        frag_tv_condition=view.findViewById(R.id.frag_tv_condition);
        frag_tv_date=view.findViewById(R.id.frag_tv_date);
        frag_tv_wind=view.findViewById(R.id.frag_tv_wind);
        frag_tv_line1=view.findViewById(R.id.frag_tv_line1);
        frag_tv_temprange=view.findViewById(R.id.frag_tv_temprange);
        frag_index_tv_dress=view.findViewById(R.id.frag_index_tv_dress);
        frag_index_tv_washcar=view.findViewById(R.id.frag_index_tv_washcar);
        frag_index_tv_cold=view.findViewById(R.id.frag_index_tv_cold);
        frag_index_tv_sport=view.findViewById(R.id.frag_index_tv_sport);
        frag_index_tv_rays=view.findViewById(R.id.frag_index_tv_rays);
        frag_iv_today=view.findViewById(R.id.frag_iv_today);
        frag_center_layout=view.findViewById(R.id.frag_center_layout);
        frag_layout=view.findViewById(R.id.frag_layout);
        frag_index_tv_dress.setOnClickListener(this);
        frag_index_tv_washcar.setOnClickListener(this);
        frag_index_tv_cold.setOnClickListener(this);
        frag_index_tv_sport.setOnClickListener(this);
        frag_index_tv_rays.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        switch (v.getId()){
            case R.id.frag_index_tv_dress:
                builder.setTitle("穿衣指数");
                Weatherbean.ResultsBean.IndexBean indexBean = indexList.get(0);
                String msg=indexBean.getZs()+"\n"+indexBean.getDes();
                builder.setMessage(msg);
                builder.setPositiveButton("确定",null);
                break;
            case R.id.frag_index_tv_washcar:
                builder.setTitle("洗车指数");
                indexBean = indexList.get(1);
                msg=indexBean.getZs()+"\n"+indexBean.getDes();
                builder.setMessage(msg);
                builder.setPositiveButton("确定",null);
                break;
            case R.id.frag_index_tv_cold:
                builder.setTitle("感冒指数");
                indexBean = indexList.get(2);
                msg=indexBean.getZs()+"\n"+indexBean.getDes();
                builder.setMessage(msg);
                builder.setPositiveButton("确定",null);
                break;
            case R.id.frag_index_tv_sport:
                builder.setTitle("运动指数");
                indexBean = indexList.get(3);
                msg=indexBean.getZs()+"\n"+indexBean.getDes();
                builder.setMessage(msg);
                builder.setPositiveButton("确定",null);
                break;
            case R.id.frag_index_tv_rays:
                builder.setTitle("紫外线强弱指数");
                indexBean = indexList.get(4);
                msg=indexBean.getZs()+"\n"+indexBean.getDes();
                builder.setMessage(msg);
                builder.setPositiveButton("确定",null);
                break;

        }
        builder.create().show();
    }
}
